package com.hongxun.budget.ui.activity;

import android.Manifest;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.widget.RadioGroup;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.fragment.MainFragment;
import com.hongxun.budget.ui.fragment.MyFragment;
import com.hongxun.budget.utils.T;

import butterknife.BindView;
import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;

public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.radioGroup)
    RadioGroup radioGroup;
    int select = 0;
    Fragment homeFragment, myFragment;

    @Override
    public  int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public  void initView(Bundle savedInstanceState) {
        selecte(select);
        radioGroup.check(R.id.home);
        radioGroup.setOnCheckedChangeListener(this);
        PermissionGen.with(this).addRequestCode(1)
                .permissions(Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION
                        ,Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE).request();
    }

    /**
     * 设置fragment显示以及切换
     *
     * @param i
     */
    private void selecte(int i) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        hideFragment(fragmentTransaction, i);
        switch (i) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new MainFragment();
                    fragmentTransaction.add(R.id.content, homeFragment);
                } else {
                    fragmentTransaction.show(homeFragment);
                }
                break;
            case 2:
                if (myFragment == null) {
                    myFragment = new MyFragment();
                    fragmentTransaction.add(R.id.content, myFragment);
                } else {
                    fragmentTransaction.show(myFragment);
                }
                break;
        }
        fragmentTransaction.commit();
    }


    private void hideFragment(FragmentTransaction ft, int i) {
        if (homeFragment != null && i != 0)
            ft.hide(homeFragment);
        if (myFragment != null && i != 2)
            ft.hide(myFragment);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
        switch (i) {
            case R.id.home:
                select = 0;
                break;
            case R.id.my:
                select = 2;
                break;
        }
        selecte(select);
    }


    @PermissionFail(requestCode = 1)
    public void requestPhotoFail() {
        T.showLong(context, "您未通过位置权限请求,将会影响点位显示，踏勘等功能");
    }
}
